package com.ruoyi.web.controller.system;


import com.ruoyi.web.service.C2CodeSubmissionActivityRankingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/C2CodeSubmissionActivityRanking")
public class C2CodeSubmissionActivityRankingController {

    @Autowired
    private C2CodeSubmissionActivityRankingService codeMergeInfoService;

    /**
     * 获取当前自然月 `fullName` 出现次数前10名
     *
     * @return 前10名数据
     */
    @GetMapping("/byMonth")
    public List<Map<String, Object>> getTop10FullNameCounts() {
        return codeMergeInfoService.getTop10FullNameCounts();
    }

    /**
     * 获取当前自然月 `moduleName` 出现次数前10名
     *
     * @return 前10名数据
     */
//    @GetMapping("/releaseByMonth")
//    public List<Map<String, Object>> getTop10ReleaseCounts() {
//        return codeMergeInfoService.getTop10WithReleaseCounts();
//    }

    /**
     * 获取过去30天 `moduleName` 出现次数前10名及对应的发布次数
     *
     * @return 包含查询结果的响应对象
     */
    @GetMapping("/releaseByMonth")
    public ResponseEntity<Map<String, Object>> getModuleWithReleaseCounts() {
        Map<String, Object> response;
        try {
            // 调用 Service 层方法获取数据
            List<Map<String, Object>> data = codeMergeInfoService.getTop10WithReleaseCounts();

            // 构造返回的 JSON 对象
            response = new HashMap<>();
            response.put("success", true);
            response.put("data", data);
            response.put("message", "Query executed successfully");

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            // 错误处理
            response = new HashMap<>();
            response.put("success", false);
            response.put("data", Collections.emptyList());
            response.put("message", "An error occurred: " + e.getMessage());

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

}
